﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;

namespace GUI
{
    public partial class frmLop : DevExpress.XtraEditors.XtraForm
    {
        #region khai bao bien
        public string maLop = "";
        DataTable dt;
        Lop_BLL lop_bll = new Lop_BLL();
        private string maDonVi = "";
        private string maHeDaoTao = "";
        private int maThamSo;
        private string maLoaiDG = "";
        TextMessageBox box = new TextMessageBox();
        private bool isOk = false;
        #endregion

        #region LoadForm

        public frmLop()
        {
            InitializeComponent();
        }
        private void frmLop_Load(object sender, EventArgs e)
        {
            LoadCombobox();
            LoadData();
        }
        #endregion

        #region cac chuc nang

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            UpdateData();

        }
        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            UpdateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region ham viet them
        private void UpdateData()
        {
            if (checkValid())
            {
                Lop_Public lop = new Lop_Public();
                if (maLop == "")
                {
                    if (checkTenInsert())
                    {
                        lop.MaLop = box.AutoID("LOP", 3, lop_bll.LayDuLieu());
                        lop.TenLop = txtTenLop.Text;
                        lop.MaDonVi = maDonVi;
                        lop.MaHe = maHeDaoTao;
                        lop.MaLoaiDocGia = maLoaiDG;
                        lop.MaThamSo = maThamSo;
                        lop.KhoaHoc = txtKhoaHoc.Text;

                        lop_bll.Insert(lop, lop, lop, lop, lop, lop, lop);

                        CloseForm();
                    }
                }
                else
                {
                    if (checkTenUpdate())
                    {
                        lop.MaLop = txtMaLop.Text.Trim();
                        lop.TenLop = txtTenLop.Text;
                        lop.MaDonVi = maDonVi;
                        lop.MaHe = maHeDaoTao;
                        lop.MaLoaiDocGia = maLoaiDG;
                        lop.MaThamSo = maThamSo;
                        lop.KhoaHoc = txtKhoaHoc.Text;

                        lop_bll.Update(lop, lop, lop, lop, lop, lop, lop);

                        CloseForm();
                    }
                }

            }
        }

        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }

        private void LoadData()
        {
            
            if (maLop != "")
            {
                string sql = "Select a.*,b.TenDonVi,c.TenHeDaoTao from DM_Lop a join DM_DonVi b on a.MaDonVi=b.MaDonVi join DM_HeDaoTao c on a.MaHe=c.MaHe where a.MaLop=N'" + maLop + "'";
                dt = lop_bll.TaoBang(sql);
                DataRow dr = dt.Rows[0];
                txtMaLop.Text = dr["MaLop"].ToString();
                txtTenLop.Text = dr["TenLop"].ToString();
                maDonVi = dr["MaDonVi"].ToString();
                //cboDonVi.Properties.DisplayMember = ;
                maHeDaoTao = dr["MaHe"].ToString();
                cboHeDaoTao.Text = dr["TenHeDaoTao"].ToString();
                txtKhoaHoc.Text = dr["KhoaHoc"].ToString();

                cboDonVi.Text=dr["TenDonVi"].ToString();
            }
        }
        private void LoadCombobox()
        {
            cboDonVi.Properties.DataSource = lop_bll.TaoBang("select MaDonVi,TenDonVi from DM_DonVi");
            cboDonVi.Properties.DisplayMember = "TenDonVi";
            cboDonVi.Properties.ValueMember = "MaDonVi";

            cboHeDaoTao.Properties.DataSource = lop_bll.TaoBang("select MaHe,TenHeDaoTao from DM_HeDaoTao");
            cboHeDaoTao.Properties.DisplayMember = "TenHeDaoTao";
            cboHeDaoTao.Properties.ValueMember = "MaHe";

            cboMaLoaiDocGia.Properties.DataSource = lop_bll.TaoBang(box.sqlSelectFile("MaLoaiDG","TenLoaiDG","DM_LoaiDocGia"));
            cboMaLoaiDocGia.Properties.DisplayMember = "TenLoaiDG";
            cboMaLoaiDocGia.Properties.ValueMember = "MaLoaiDG";

            cboMaThamSo.Properties.DataSource = lop_bll.TaoBang(box.sqlSelectFile("MaThamSo,TenThamSo","SoLuongMuon","ThamSo"));
            cboMaThamSo.Properties.DisplayMember = "TenThamSo";
            cboMaThamSo.Properties.ValueMember = "MaThamSo";

        }

        private bool checkValid()
        {
            //if (txtMaLop.Text.Trim() == "")
            //{
            //    XtraMessageBox.Show("Bạn chưa nhập Mã lớp.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            //    txtMaLop.Focus();
            //    return false;
            //}
            if (txtTenLop.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên lớp.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenLop.Focus();
                return false;
            }
            if (cboDonVi.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Đơn vị.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboDonVi.Focus();
                return false;
            }
            if (cboHeDaoTao.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Hệ đào tạo.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboHeDaoTao.Focus();
                return false;
            }

            if (cboMaLoaiDocGia.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Loại độc giả.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboMaLoaiDocGia.Focus();
                return false;
            }
            if (cboMaThamSo.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa chọn Tham Số.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                cboMaThamSo.Focus();
                return false;
            }
            if (txtKhoaHoc.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập khóa học.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtKhoaHoc.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenInsert()
        {
            string sql = "select * from DM_Lop where TenLop=N'"+txtTenLop.Text.Trim()+"' and KhoaHoc='"+txtKhoaHoc.Text+"'";
            dt = lop_bll.TaoBang(sql);
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(string.Format("Tên lớp: {0} với Khóa học:'"+txtKhoaHoc.Text+"' đã tồn tại.Hãy nhập lại.",txtTenLop.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenLop.Focus();
                return false;
            }
            return true;
        }
        private bool checkTenUpdate()
        {
            string sql = "select * from DM_Lop where TenLop=N'" + txtTenLop.Text.Trim() + "' and KhoaHoc='" + txtKhoaHoc.Text + "'";
            dt = lop_bll.TaoBang(sql);
            if (dt.Rows.Count+1 >1)
            {
                XtraMessageBox.Show(string.Format("Tên lớp: {0} với Khóa học:'" + txtKhoaHoc.Text + "' đã tồn tại.Hãy nhập lại.", txtTenLop.Text), box.Caption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtTenLop.Focus();
                return false;
            }
            return true;
        }
        private void SetInterface()
        {
            txtMaLop.Text = "";
            txtTenLop.Text = "";
            cboDonVi.Text = "";
            cboHeDaoTao.Text = "";          
            txtKhoaHoc.Text = "";
            maDonVi = "";
            maHeDaoTao = "";
            maLoaiDG = "";
            maLop = "";
            txtMaLop.Text = "Mã tự sinh";
            txtTenLop.Focus();
        }
        #endregion

        #region cac su kien

        private void frmLop_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                this.DialogResult = DialogResult.OK;
        }

        private void cboMaLoaiDocGia_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maLoaiDG = cboMaLoaiDocGia.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboMaThamSo_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maThamSo =box.ToInt(cboMaThamSo.EditValue.ToString());

            }
            catch (Exception)
            { }
        }

        private void cboDonVi_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maDonVi = cboDonVi.EditValue.ToString();
            }
            catch (Exception)
            { }
        }

        private void cboHeDaoTao_TextChanged(object sender, EventArgs e)
        {
            try
            {
                maHeDaoTao = cboHeDaoTao.EditValue.ToString();
            }
            catch (Exception)
            { }
        }
        #endregion

    }
}